<?php
namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
//use app\shop\model\Artist as BannerModel;
use app\shop\model\AccountLog as AccountLogModel;
use app\shop\model\Banner as BannerModel;
use app\shop\model\User;
use think\Hook;

/**
 * 充值控制器
 */
class AccountLog extends Admin
{
    public function index()
    {
    	//记录call_back
    	cookie('__forward__', $_SERVER['REQUEST_URI']);

        $data_list = AccountLogModel::order('sort,id desc')->paginate();
        $user = User::column('id,name');
        $state = array(0=>'未知',1=>'待审核',2=>'审核通过',3=>'审核不通过');
        $type = array(1=>'充值',2=>'充值赠送');
        $show = [
            'title' => '审核充值内容',
            'icon' => 'fa fa-plus-circle',
            'target' => false,
            'href' => url('shop/AccountLog/edit' ,['id' => '__id__'])
        ];
        return ZBuilder::make('table')
            ->setPageTitle('充值管理')
//            ->setSearch(['id' => 'ID', 'title' => '头衔', 'name' => '姓名'])
            ->addColumns([
                ['id', 'ID'],
                ['user_id', '用户',$user],
                ['status', '状态',$state],
//                ['type', '类型',$type],
                ['money', '金额'],
                ['sale_money', '赠送金额'],
                ['create_time', '创建时间', 'datetime'],
                ['right_button', '操作', 'btn']
            ])

            ->addTopButtons('enable,disable,delete') // 批量添加顶部按钮
            ->addRightButtons('edit,delete') // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染页面
    }

    public function add()
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Banner');
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if ($user = AccountLogModel::create($data)) {
                Hook::listen('banner_user_add', $user);
                // 记录行为
                action_log('banner_user_add', 'artist_user_add', $user['id'], UID);
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }

        return ZBuilder::make('form')
            ->setPageTitle('新增充值')
            ->addFormItems([
                ['text', 'title', '标题', '必填'],
                ['text', 'sort', '排序', '', 100],
                ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
            ])
            ->fetch();
    }

    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(!isset($data['status']) || !$data['status']){
                $this->error('未选择审核状态,请确认重试');
            }
            if(!isset($data['sale_money']) || !is_numeric($data['sale_money'])){
                $this->error('未输入赠送金额,请确认重试');
            }
            if($data['status'] == 2){//更新账户余额
                $oldmoney = User::where('id',$data['user_id'])->value('money');
                if(!$oldmoney){
                    $money = $data['money'] + $data['sale_money'];
                }else{
                    $money = $oldmoney + $data['money'] + $data['sale_money'];
                }
//                pr($money);
//                pr();exit;
                $upMoney = User::where(['id'=>$data['user_id']])->update(['money'=>$money]);
//                pr($upMoney);
                if(!$upMoney){
                    $this->error('账户余额更新失败，请确认重试');
                }
//                echo $money;exit;
            }
            $data['admin_id'] = UID;
            if (AccountLogModel::update($data)) {
                $user = AccountLogModel::get($data['id']);
                Hook::listen('banner_user_edit', $user);
                $this->success('更新成功', cookie('__forward__'));
            } else {
                $this->error('更新失败');
            }
        }
        $user = User::column('id,name');
        $state = array(2=>'审核通过',3=>'审核不通过');//0=>'未知',1=>'提交',
        // 获取数据
        $info = AccountLogModel::where('id', $id)->find();
//        pr($info);
        // 使用ZBuilder快速创建表单
        if($info->status ==1 || $info->status ==3){
            return ZBuilder::make('form')
                ->setPageTitle('审核充值内容') // 设置页面标题
                ->addFormItems([ // 批量添加表单项
                    ['hidden', 'id'],

                    ['hidden', 'money', '充值金额'],
                    ['hidden','user_id'],
                    ['static','name','姓名','',User::where('id',$info['user_id'])->value('name')],
                    ['static', 'money', '充值金额'],
                    ['number', 'sale_money', '赠送金额'],
                    ['textarea', 'remark', '备注信息'],
                    ['radio', 'status', '状态', '', $state]
                ])
                ->setFormData($info) // 设置表单数据
                ->fetch();
        }else{

            $state = array(0=>'未知',1=>'待审核',2=>'审核通过',3=>'审核不通过');//
            return ZBuilder::make('form')
                ->setPageTitle('查看充值内容') // 设置页面标题
                ->addFormItems([ // 批量添加表单项
                    ['hidden', 'id'],

                    ['hidden','user_id'],
                    ['static','name','姓名','',User::where('id',$info['user_id'])->value('name')],
                    ['static', 'money', '充值金额'],
                    ['static', 'sale_money', '赠送金额'],
                    ['static', 'remark', '备注信息'],
                    ['static','st','审核状态','',$state[$info['status']]],
                ])
                ->setFormData($info) // 设置表单数据
                ->hideBtn('submit')
                ->fetch();
        }
    }

    public function delete($ids = [])
    {
        Hook::listen('artist_user_delete', $ids);
        return $this->setStatus('delete');
    }


    /**
     * 设置用户状态：删除、禁用、启用
     */
    public function setStatus($type = '', $record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $uid_delete = is_array($ids) ? '' : $ids;
        $ids        = array_map('get_nickname', (array)$ids);
        return parent::setStatus($type, ['user_'.$type, 'shop_artist', $uid_delete, UID, implode('、', $ids)]);
    }
}

